// pages/myPages/setPage/setPage.js
var wallet = require('../../wallet.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    setLang:{},
    storageDataSize:'',
    key:[],
    show: 0,
    PlayShow: false,
    playData: [],
    payPSWD: '',
    arrData: [],
    aginpswd: '',
    textList: [1, 2, 3, 4, 5, 6],
    helpWordShow: false,
    PrativeKeyFlag: 0,
    id: 0,
    isKey: '',
    prativeKey: ''
  },
 getStorageSize(){
   let $this = this;
   wx.getStorageInfo({
     success: function (res) {
       //console.log(res.keys)
       //console.log(res.currentSize)
      let key =   res.keys.filter((res,index)=>{
        return res != 'pswd' && res != 'walletKey' && res != 'addressETH' && res != 'createUser' && res != 'helpWord'
       })
        $this.setData({
          storageDataSize: key.length == 0 ? '0kb' : res.currentSize+'kb',
          key: key
        })
     }
   })
 },
 removeData(){
   this.data.key.map((res,index)=>{
     wx.removeStorageSync(res)
   })
   this.setData({
     key:[],
     storageDataSize:'0kb'
   })
 },
  PlayKnow() {
    this.setData({
      PlayShow: false,
      helpWordShow: false
    })
  },
  shuruEvent: function (ev) {
    //console.log(ev.detail.value.length);
    switch (ev.detail.value.length) {
      case 0:
        this.setData({
          arrData: []
        })
        break;
      case 1:
        this.setData({
          arrData: [0]
        })
        break;
      case 2:
        this.setData({
          arrData: [0, 1]
        })
        break;
      case 3:
        this.setData({
          arrData: [0, 1, 2]
        })
        break;
      case 4:
        this.setData({
          arrData: [0, 1, 2, 3]
        })
        break;
      case 5:
        this.setData({
          arrData: [0, 1, 2, 3, 4]
        })
        break;
      case 6:
        this.setData({
          arrData: [0, 1, 2, 3, 4, 5]
        })
        break;
      default:
        break;
    }
    this.setData({
      payPSWD: ev.detail.value
    })
    //console.log(ev.detail.value)
  },
  exportPrative(id) {//解密钱包
    let hdwallet = wx.getStorageSync('walletKey')
    let pswd = wx.getStorageSync('pswd')
    let BTC = wallet.BTCNET
    let ETH = wallet.ETHNET
    hdwallet = wallet.decode(pswd, hdwallet, 0)
    if (id == 1) {
      let BTCWallet = hdwallet.derive(BTC)
      let prativeBTC = BTCWallet._privateKey.toString('hex')
      this.setData({
        prativeKey: prativeBTC
      })
    } else if (id == 2) {
      let ETHWallet = hdwallet.derive(ETH)
      let prativeETH = ETHWallet._privateKey.toString('hex')
      this.setData({
        prativeKey: prativeETH
      })
    } else {
      return 0;
    }

  },
  exportEth(ev) {//导eth
    this.setData({
      //PrativeKeyFlag:1,
      PlayShow: true,
      id: ev.currentTarget.id,
      isKey: this.data.setLang.ETHKEY
    })
    this.exportPrative(ev.currentTarget.id)
  },
  exportBTC(ev) {
    //console.log(1)
    this.setData({
      //PrativeKeyFlag:1,
      PlayShow: true,
      id: ev.currentTarget.id,
      isKey: this.data.setLang.BTCKEY
    })
    //console.log(ev)
    this.exportPrative(ev.currentTarget.id)
  },
  copyKey() {//复制私钥
    let data = this.data.prativeKey;
    let setLang = this.data.setLang
    wx.setClipboardData({
      data: data,
      success: function (res) {
        //console.log(res)
        wx.showToast({
          title: setLang.copySuccess,
          icon: 'none',
          duration: 300
        })
      }
    })
  },
  commitPswd() {
    //console.log(this.data.payPSWD)
    let pswd = this.data.payPSWD
    pswd = wallet.MD5jiami(pswd)
    let localPswd = wx.getStorageSync('pswd');
    if (pswd == localPswd) {
      //console.log(pswd)
      this.setData({
        PlayShow: false,
        payPSWD: '',
        arrData: [],
        helpWordShow: this.data.id == 0 ? true : false,
        PrativeKeyFlag: this.data.id
      })
      //console.log(this.data.id)
    } else {
      wx.showToast({
        title: wx.T.locale == "zh-Hans" ? wx.T.locales['zh-Hans'].paymentPassword : wx.T.locales['en'].paymentPassword,
        icon: 'none',
        duration: 2000
      })
    }
  },
  PlayKnow() {
    this.setData({
      PlayShow: false,
      helpWordShow:false
    })
  },
  closePswd() {
    this.setData({
      PlayShow: false,
      arrData: []
    })
  },
  closeKey() {
    this.setData({
      PrativeKeyFlag: 0
    })
  },
  GoExportWord(ev) {
    // console.log(11)
    let word = wx.getStorageSync('helpWord');
    let pswd = wx.getStorageSync('pswd');
    if (word && pswd) {
      let helpWord = wallet.decode(pswd, word, true);
      //console.log(helpWord)
      this.setData({
        PlayShow: true,
        id: ev.currentTarget.id,
        playData: helpWord.split(' ')
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
     
    this.getStorageSize && this.getStorageSize()
     
  },
  changePassWordPage: function (e) {
    wx.navigateTo({
      url: './changePassWordPage/changePassWordPage'
    })
  },
  HandoverInChineseAndEnglish: function (e) {
    wx.navigateTo({
      url: './HandoverInChineseAndEnglish/HandoverInChineseAndEnglish'
    })
  },
  webNatPage:function(e){
    var current = e.currentTarget.dataset.current;
    if (current == 1){
      wx.navigateTo({
        url: './webNatPage/webNatPage?id=1'
      })
    } else if (current == 2){
      wx.navigateTo({
        url: './webNatPage/webNatPage?id=2'
      })
    }
  },
  logOut:function(e){
    wx.clearStorage()
    wx.reLaunch({
      url: '../../../pages/lead/leadIndex'
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
   
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      setLang: wx.T.locale == "zh-Hans" ? wx.T.locales['zh-Hans'] : wx.T.locales['en']

    })
    wx.setNavigationBarTitle({
      title: wx.T.locale == "zh-Hans" ? wx.T.locales['zh-Hans'].setted : wx.T.locales['en'].setted
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})